package com.ccys.cms.service.formulation.impl;

import com.ccys.cms.service.formulation.FormulationService;
import com.ccys.cms.service.formulation.QuantityTypeService;
import com.ccys.common.vo.ResponseResult;
import com.ccys.db.dto.web.formulation.QuantityTypeDto;
import com.ccys.db.dto.web.productAndService.NumEnum;
import com.ccys.db.entity.TblFormulation;
import com.ccys.db.entity.TblFormulationQuantityType;
import com.ccys.db.mapper.TblFormulationQuantityTypeMapper;
import com.ccys.db.page.PageDto;
import com.ccys.db.rely.service.impl.BaseServiceImpl;
import com.ccys.db.vo.web.formulation.QuantityTypeVo;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


@Service
@Slf4j
public class QuantityTypeServiceImpl extends BaseServiceImpl<TblFormulationQuantityType> implements QuantityTypeService {

    @Resource
    private FormulationService formulationService;

    @Resource
    private TblFormulationQuantityTypeMapper quantityTypeMapper;

    @Override
    public ResponseResult getPage(QuantityTypeDto dto) {
        PageDto.startPage();
        List<QuantityTypeVo> list = quantityTypeMapper.getList(dto);
        PageInfo<QuantityTypeVo> info = new PageInfo<>(list);
        return ResponseResult.success(info);
    }

    @Override
    public ResponseResult change(TblFormulationQuantityType model) {
        //新增/修改的时候判断formulation允不允许复数个quantitytype
        //1、允许直接新增/修改
        //2、如果不允许就看是否已经有了，没有就新增/修改，有就抛出异常
        //查询设置
        TblFormulation entityById = formulationService.findEntityById(model.getFormulationId());
        if(Objects.nonNull(entityById)){
            if (Objects.nonNull(entityById.getAllowMultiple())&&NumEnum.NOT_ALLOW.getCode().equals(entityById.getAllowMultiple())) {
                //查询已有数量
                List<TblFormulationQuantityType> list = this.findListByLimit(TblFormulationQuantityType.class, new ArrayList<String>() {{
                    add(" del_flag = 0 ");
                    add(" formulation_id =  '" + model.getFormulationId() + "'");
                    add(" formulation_doctor_id = '" + model.getFormulationDoctorId() + "'");
                    add(" formulation_patient_id = '" + model.getFormulationPatientId() + "'");
                    if (StringUtils.isNotEmpty(model.getId())) {
                        add(" id != " + model.getId());
                    }

                }}, "created_on_utc desc");
                if (CollectionUtils.isNotEmpty(list)) {
                    return ResponseResult.error("formulation allow multiple UOM is false");
                }
            } else {
                List<TblFormulationQuantityType> list = this.findListByLimit(TblFormulationQuantityType.class, new ArrayList<String>() {{
                    add(" del_flag = 0 ");
                    add(" formulation_id =  '" + model.getFormulationId() + "'");
                    add(" formulation_doctor_id = '" + model.getFormulationDoctorId() + "'");
                    add(" formulation_patient_id = '" + model.getFormulationPatientId() + "'");
                    //同一个UOM不出现多次
                    add(" uom_id = '" + model.getUomId() + "'");
                    if (StringUtils.isNotEmpty(model.getId())) {
                        add(" id != '" + model.getId()+"'");
                    }

                }}, "created_on_utc desc");
                if (CollectionUtils.isNotEmpty(list)) {
                    return ResponseResult.error("This UOM already exists");
                }
            }
        }

        int i = this.insertOrUpdate(model);
        return ResponseResult.update(i);

    }
}
